package com.guaju.yueshangLife.adapter;

import android.app.Activity;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.databinding.FooterHomeItemBinding;
import com.guaju.yueshangLife.recyclerView.BaseRecyclerAdapter;
import com.guaju.yueshangLife.recyclerView.BaseRecyclerHolder;

import java.util.List;

public class SalesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context context;
    List list;
    private int status = 1;

    public static final int LOAD_NONE = 2;
    public static final int LOAD_END = 3;
    private static final int TYPE_FOOTER = -2;
    private static final int TYPE_CONTENT = -4;

    Activity activity;
    private final LayoutInflater layoutInflater;
    private View view;

    public SalesAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
        activity = (Activity) context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int type) {

        //选择类型
        RecyclerView.ViewHolder result = null;
        switch (type) {
            case LOAD_NONE:
                view = layoutInflater.inflate(R.layout.item_sales, parent, false);
//                result = new LectureHolder(view);
                break;
            case TYPE_FOOTER:
                FooterHomeItemBinding mBindFooter =
                        DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.footer_home_item, parent, false);
                result = new FooterViewHolder(mBindFooter.getRoot());
                break;
        }
        return result;
    }

    //绑定
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder == null) {
            return;
        }
        if (holder instanceof FooterViewHolder) {
            FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
            footerViewHolder.bindItem();
        } else {
        }
    }

    //有多少个item？
    @Override
    public int getItemCount() {
        return 1 + list.size();
    }

    //item类型
    @Override
    public int getItemViewType(int position) {
        int type = TYPE_CONTENT;
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return type;
        }

    }


    public interface OnItemClickListener {
        void onItemClick(View v, int position);

        void onItemLongClick(View v);
    }

    public SalesAdapter.OnItemClickListener mOnItemClickListener;//第二步：声明自定义的接口

    //第三步：定义方法并暴露给外面的调用者
    public void setOnItemClickListener(SalesAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    class StudioHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        SimpleDraweeView studio_avatar;
        TextView studio_name, studio_address2time, student_num, studio_lastnum;
        String studio_id;

        public StudioHolder(View itemView) {
            super(itemView);
            
            itemView.setOnClickListener(this);

        }

        public void setContent() {
            Log.e("guaju", "setContent: " + studio_name + studio_address2time + student_num + studio_lastnum + studio_avatar);
        }

        @Override
        public void onClick(View v) {
        }
    }


    /**
     * footer view
     */
    private class FooterViewHolder extends RecyclerView.ViewHolder {

        FooterHomeItemBinding mBindFooter;

        FooterViewHolder(View itemView) {
            super(itemView);
            mBindFooter = DataBindingUtil.getBinding(itemView);
            mBindFooter.rlMore.setGravity(Gravity.CENTER);
        }

        private void bindItem() {
            switch (status) {
                case LOAD_NONE:
                    mBindFooter.progress.setVisibility(View.GONE);
                    mBindFooter.tvLoadPrompt.setText("暂无数据");
                    mBindFooter.tvLoadPrompt.setVisibility(View.VISIBLE);
                    mBindFooter.homeBottom.setVisibility(View.INVISIBLE);
                    break;
                case LOAD_END:
                    mBindFooter.progress.setVisibility(View.GONE);
                    mBindFooter.tvLoadPrompt.setVisibility(View.GONE);
                    mBindFooter.homeBottom.setVisibility(View.VISIBLE);
                    itemView.setVisibility(View.GONE);
                default:
                    break;
            }
        }

    }

    /**
     * 这里规定 position =  getItemCount() - 1 时
     * 就为尾布局，设置为占满整屏幕宽
     * getItemCount() 改了 ，这里就不用改
     */
    private boolean isFooter(int position) {
        return position < getItemCount() && position >= getItemCount() - 1;
    }

    public void updateLoadStatus(int status) {
        this.status = status;
        notifyDataSetChanged();
    }

    public int getLoadStatus() {
        return this.status;
    }


}
